import QtQuick 1.1
import com.nokia.meego 1.0
import "Logic.js" as Logic

Page {
    id: pageStartLearning

    property string _word: ""
    property string _translation: ""


    tools: ToolBarLayout {
        id: tool_only_back
        opacity: userTheme.opacityToolbar
        ToolIcon {
            id: toolIconBack
            //iconId: "toolbar-back"
            iconSource: "images/toolbar/back.png"
            onClicked: {
                pageStack.pop();
            }
        }
        ToolIcon {
            id: toolIconForward
            //iconId: "toolbar-done"
            iconSource: "images/toolbar/ok.png"
            onClicked: {
                if ( dlgSelectCategory.selectedIndexes.length === 0 ) {
                    dlgSelCat.text = qsTr("Select a categories of words")
                    dlgSelCat.open()
                } else {
                    /* обнуление счетчика */
                    appWindow.answTrue = 0
                    appWindow.answFalse = 0

                    /* запуск окна с обучение слов */
                    var dL = dlgDirectLearn.directionLearnIndx
                    appWindow.bufWords = Logic.getAllWordsLearn( dL )
                    appWindow.siz_bufWords = Logic.getNumWordsLearn()
                    appWindow.porgressLearn = 0

                    var objRes = Logic.getWord( appWindow.bufWords )
                    if( objRes !== undefined) {
                        appWindow.bufWords = objRes.buf

                        appWindow.learnWord = objRes.obj.word
                        appWindow.learnTranslation = objRes.obj.translation

                        appWindow.curShowTrue = objRes.obj.strue
                        appWindow.curShowFalse = objRes.obj.sfalse
                        appWindow.curWordId = objRes.obj.id

                        //listModelErrorWordsTest.clear()
                        var bufWE = new Array()
                        appWindow.bufWordsErrorTest = bufWE

                        /* открываем окно */
                        Logic.openFile("Learning.qml")
                    } else {
                        /* нет слов для показа */
                        dlgSelCat.text = qsTr("The selected category does not contain any words")
                        dlgSelCat.open()
                    }


                }
            }
        }
    } //ToolBarLayout

    /*************************************************/
    function getAllCategories_old() {
        var db = Logic.getDatabase()

        listModelCategories.clear()
        btnSelectCategories.enabled = false

        db.transaction( function(tx) {
                           var res = tx.executeSql( "SELECT id,category FROM categories" )
                           for(var i = 0; i<=res.rows.length-1; i++) {
                               btnSelectCategories.enabled = true
                               var _id = res.rows.item(i).id;
                               var nameCategory = res.rows.item(i).category;
                               //var descrCategory = res.rows.item(i).description;
                               listModelCategories.append( { name: nameCategory, category_id: _id } );
                           }
                       }
                    )
        /* установка значение для первого элемента */
        //titlePage.busy = false
    }

    function getAllCategories() {
        var db = Logic.getDatabase()

        listModelCategories.clear()
        btnSelectCategories.enabled = false

        db.transaction( function(tx) {
                           var res = tx.executeSql ( ""+
                                                   "SELECT c.category, c.description, c.id, count(w.category_id) AS cnt FROM categories c " +
                                                   "LEFT JOIN words w ON c.id = w.category_id " +
                                                   "GROUP BY c.id" +
                                                   "" )

                           for(var i = 0; i<=res.rows.length-1; i++) {
                               btnSelectCategories.enabled = true
                               var id = res.rows.item(i).id;
                               var nameCategory = res.rows.item(i).category;
                               var descrCategory = res.rows.item(i).description;
                               var numw = res.rows.item(i).cnt;
                               listModelCategories.append( { name: nameCategory, category_id: id, words: numw } );
                           }
                       }
                    )
    }

    /*************************************************/

    ListModel {
        id: listModelCategories
        Component.onCompleted: {
            getAllCategories()
        }
    }

    QNMTitle {
        id: titlePage
        text: qsTr("Learn words")
        image: "images/learn.png"
        busy: false
    }

    BackgroundPage { }

    Item {
        id: panelTopLeft
        height: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.height - titlePage.height : (parent.height - titlePage.height)/2
        width: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.width/2  : parent.width
        anchors.top: titlePage.bottom
        anchors.left: titlePage.left

        Text {
            id: txtCat
            text: qsTr("Categories")
            anchors.top: parent.top
            anchors.topMargin: 30
            anchors.left: parent.left
            anchors.leftMargin: 10
            font.pixelSize: 30
        }

        MyButton {
            id: btnSelectCategories
            iconSource: "images/categories_icon.png"
            text: qsTr("Selected:") + " " + dlgSelectCategory.selectedIndexes.length
            width: parent.width-20
            height: 100
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: txtCat.bottom
            anchors.topMargin: 20
            onClicked: {
                dlgSelectCategory.open()
            }
        }

        MyButton {
            id: directLearn
            iconSource: "images/circle_arrow.png"
            width: parent.width-20
            height: 100
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: btnSelectCategories.bottom
            anchors.topMargin: 20
            text: dlgDirectLearn.directionLearnTxt
            onClicked: {
                dlgDirectLearn.open()
            }
        }
    }
    /*---------------------- */
    Item {
        id: panelBottomRight
        height: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.height - titlePage.height : (parent.height - titlePage.height)/2
        width: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? parent.width/2  : parent.width
        anchors.top: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? titlePage.bottom : panelTopLeft.bottom
        anchors.left: screen.orientationString == "Landscape" || screen.orientationString == "LandscapeInverted" ? panelTopLeft.right : parent.left

        Text {
            id: txtListSel
            text: qsTr("Selected categories")
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.left: parent.left
            anchors.leftMargin: 10
            font.pixelSize: 30
        }

        ListView {
            id: lvSelectedCategories
            anchors.top: txtListSel.bottom
            anchors.topMargin: 5
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 5
            width: parent.width - 20
            anchors.horizontalCenter: parent.horizontalCenter

            Component {
                id: compItemListCategories
                Rectangle {
                    width: lvSelectedCategories.width
                    height: 70
                    border.color: "grey"
                    radius: 10
                    clip: true
                    gradient: Gradient {
                        GradientStop { position: 0; color: "transparent" }
                        GradientStop { position: 1; color: "lightgrey" }
                    }

                    Row {
                        anchors.verticalCenter: parent.verticalCenter
                        spacing: 20
                        Image {
                            id: imgItem
                            source: "images/pushpin.png"
                            smooth: true
                            height: parent.height
                            width: parent.height
                        }
                        /*Text {
                            text: name
                            font.pixelSize: 30
                        } */
                        Column {
                                    spacing: 5
                                    anchors.verticalCenter: parent.verticalCenter
                                    Text {
                                        id: txtNameCategory;
                                        text: name; font.pixelSize: 28;
                                    }
                                    Text {
                                        id: txtWordsInCategory;
                                        text: qsTr("Words") + ": " + words;
                                        font.pixelSize: 16;
                                    }
                                }
                    }

                }
            }

            model: listModelSelectedCategories
            delegate: compItemListCategories
            clip: true
        }

    }

    /*************************************************/


    ListModel {
        id: listModelSelectedCategories
    }

    QueryDialog {
        id: dlgDirectLearn
        property string directionLearnTxt: b_wt.text
        property int directionLearnIndx: 0
        title: Item {
            height: 140
            width: 140
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            Image {
                anchors.centerIn: parent
                source: "images/circle_arrow.png"
            }
        }

        content: ButtonColumn {
            width: parent.width
            height: 240
            anchors.horizontalCenter: parent.horizontalCenter
            Button {
                id: b_wt
                checkable: dlgDirectLearn.directionLearnIndx === 0
                text: qsTr("Word - Translation")
                height: 100
                onClicked: {
                    dlgDirectLearn.directionLearnTxt = b_wt.text
                    dlgDirectLearn.directionLearnIndx = 0
                    dlgDirectLearn.close()
                }
            }
            Button {
                id: b_tw
                checkable: dlgDirectLearn.directionLearnIndx === 1
                text: qsTr("Translation - Word")
                height: 100
                onClicked: {
                    dlgDirectLearn.directionLearnTxt = b_tw.text
                    dlgDirectLearn.directionLearnIndx = 1
                    dlgDirectLearn.close()
                }
            }
        }
    }

    MultiSelectionDialog {
        id: dlgSelectCategory
        titleText: qsTr("Selected categories")
        model: listModelCategories
        acceptButtonText: "Ok"

        onAccepted: {
            var lSel = selectedIndexes.length
            Logic.clearCategoryLearn()
            listModelSelectedCategories.clear()
            for ( var j=0; j<lSel; j++ ) {
                listModelSelectedCategories.append( {
                                                       name: dlgSelectCategory.model.get( dlgSelectCategory.selectedIndexes[j] ).name,
                                                       words: dlgSelectCategory.model.get( dlgSelectCategory.selectedIndexes[j] ).words
                                                   } );
                var category_id = dlgSelectCategory.model.get( dlgSelectCategory.selectedIndexes[j] ).category_id
                Logic.addCategoryLearn( category_id )
            }
        }
    }


    QueryDialog {
        id: dlgSelCat
        title: Image {
            anchors.horizontalCenter: parent.horizontalCenter
            //source: "image://theme/icon-l-user-guide"
            source: "images/info.png"
        }
        property alias text: dlgSelCat.message
        message: qsTr("Select a category of words")
    }

}
